package com.google.code.p.restrpc.core.url;

import org.apache.commons.lang.StringUtils;

import com.google.code.p.restrpc.MutableRpcRequest;
import com.google.code.p.restrpc.RequestData;
import com.google.code.p.restrpc.RpcProtocal;
import com.google.code.p.restrpc.RpcRequest;
import com.google.code.p.restrpc.TransferFormat;

/**
 * @author dz
 */
public abstract class AbstractRpcRequest implements MutableRpcRequest {
    
    String url;
    
    RpcProtocal protocal;
    
    TransferFormat transferFormat;
    
    String context;
    
    String address;
    
    int port = 80;
    
    int weight = 1;
    
    String serviceName;
    
    String methodName;
    
    boolean async;
    
    String requestId;
    
    Object body;
    
    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }
    
    /**
     * @param url
     *            the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }
    
    /**
     * @return the protocal
     */
    public RpcProtocal getProtocal() {
        return protocal;
    }
    
    /**
     * @param protocal
     *            the protocal to set
     */
    public void setProtocal(RpcProtocal protocal) {
        this.protocal = protocal;
    }
    
    /**
     * @return the transferFormat
     */
    public TransferFormat getTransferFormat() {
        return transferFormat;
    }
    
    /**
     * @param transferFormat
     *            the transferFormat to set
     */
    public void setTransferFormat(TransferFormat transferFormat) {
        this.transferFormat = transferFormat;
    }
    
    /**
     * @return the context
     */
    public String getContext() {
        return context;
    }
    
    /**
     * @param context
     *            the context to set
     */
    public void setContext(String context) {
        this.context = context;
    }
    
    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }
    
    /**
     * @param address
     *            the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }
    
    /**
     * @return the port
     */
    public int getPort() {
        return port;
    }
    
    /**
     * @param port
     *            the port to set
     */
    public void setPort(int port) {
        this.port = port;
    }
    
    /**
     * @return the weight
     */
    public int getWeight() {
        return weight;
    }
    
    /**
     * @param weight
     *            the weight to set
     */
    public void setWeight(int weight) {
        this.weight = weight;
    }
    
    /**
     * @return the serviceName
     */
    public String getServiceName() {
        return serviceName;
    }
    
    /**
     * @param serviceName
     *            the serviceName to set
     */
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
    
    /**
     * @return the methodName
     */
    public String getMethodName() {
        return methodName;
    }
    
    /**
     * @param methodName
     *            the methodName to set
     */
    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }
    
    /**
     * @return the async
     */
    public boolean isAsync() {
        return async;
    }
    
    /**
     * 
     */
    public void setAsync(boolean async) {
        this.async = async;
    }
    
    /**
     * @return the requestId
     */
    public String getRequestId() {
        return requestId;
    }
    
    /**
     * @param requestId
     *            the requestId to set
     */
    public void setRequestId(String requestId) {
        this.async = StringUtils.isNotEmpty(requestId);
        this.requestId = requestId;
    }
    
    /**
     * @return the body
     */
    public RequestData getData() {
        RequestData result = new RequestData();
        if (async) {
            AsyncHeader asyncHeader = new AsyncHeader();
            asyncHeader.setAsync(true);
            asyncHeader.setRequestId(requestId);
            result.getHead().put(ASYNC_KEY, asyncHeader);
        }
        result.setBody(body);
        return result;
    }
    
    /**
     * @param body
     *            the body to set
     */
    public void setBody(Object body) {
        this.body = body;
    }
    
}
